﻿using SilentOrbit.ProtocolBuffers;
using System;
using System.IO;

namespace CombineProto
{
    class Program
    {
        static int Main(string[] args)
        {
            GenerateConfig config = new GenerateConfig();
            if (args!=null)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    var split = args[i].Split('=');
                    if (split.Length != 2)
                    {
                        Console.Write("args is error: {0}", args[i]);
                        continue;
                    }
                    string fieldName = split[0];
                    string value = split[1];
                    var fieldInfo = config.GetType().GetField(fieldName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                    if (fieldInfo == null)
                    {
                        Console.Write("Don't has this param:" + fieldName);
                        return 1;
                    }
                    fieldInfo.SetValue(config, value);
                }
            }
            if (config.cmdIsEnum == "0")
            {
                MessageToProto proto = new MessageToProto(config);
                proto.GenerateAll();
            }
            else
            {
                EnumToProto proto = new EnumToProto(config);
                proto.GenerateAll();

                if (config.useCodeGenerator)
                {
                    if(File.Exists(config.luaAnnotationPath))
                    {
                        var backupFile = config.luaAnnotationPath + ".bak";
                        if (File.Exists(backupFile))
                            File.Delete(backupFile);
                        File.Move(config.luaAnnotationPath, backupFile);
                    }

                    var generator = new ProtoAnnotationGenerator();
                    generator.Generate(config.sourceProtoRelatePath, config.luaAnnotationPath);

                }
            }
            return 0;
        }
    }
}
